package com.example.taxfloatproject20240926.ui;


import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;

import androidx.activity.ComponentActivity;
import androidx.databinding.DataBindingUtil;

import com.example.taxfloatproject20240926.R;

import com.example.taxfloatproject20240926.databinding.ActivityMainBinding;
import com.example.taxfloatproject20240926.ui.busevent.BusProvider;
import com.example.taxfloatproject20240926.ui.event.ConnEvent;
import com.example.taxfloatproject20240926.ui.event.MsgEvent;
import com.example.taxfloatproject20240926.ui.floatrecorder.FloatControlView;
import com.example.taxfloatproject20240926.ui.floatrecorder.MainActivityViewModel;
import com.example.taxfloatproject20240926.ui.socket.CmdUtils;
import com.example.taxfloatproject20240926.ui.socket.NettyTCPClient;
import com.example.taxfloatproject20240926.ui.utils.Constant;
import com.example.taxfloatproject20240926.ui.utils.PreferencesUtils;
import com.squareup.otto.Subscribe;


public class MainActivity extends ComponentActivity implements FloatControlView.OnSureClickListener,
        FloatControlView.OnOpenSetActivityListener {

    MainActivityViewModel viewModel;
    private NettyTCPClient mNettyClient;

    ConnectNetReciver connectReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        BusProvider.getInstance().register(this);
        ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this,
                R.layout.activity_main);
        viewModel = new MainActivityViewModel(this,mainBinding,this,this);
        mainBinding.setViewModel(viewModel);
        MyApp.getInstance().addActivity(this);
        moveTaskToBack(true);
        registerReciverCon();
        if (!initConfig()) return;
        initSocketClient(Constant.server_ip, Constant.eval_port);

    }

    private void registerReciverCon() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        connectReceiver = new ConnectNetReciver();
        registerReceiver(connectReceiver, intentFilter);
    }
    private boolean initConfig() {
        Constant.server_ip = PreferencesUtils.getString(this, Constant.server_ip_key);
        Constant.eval_addr = PreferencesUtils.getString(this, Constant.eval_addr_key);
        Constant.eval_port = PreferencesUtils.getInt(this, Constant.eval_port_key);
        Constant.yl = PreferencesUtils.getString(this, Constant.yl_key);
        if (!TextUtils.isEmpty(Constant.server_ip)
                && !"127.0.0.1".equals(Constant.server_ip)
        ) {
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusProvider.getInstance().unregister(this);
        if (viewModel != null) {
            viewModel.release();
        }

        if (mNettyClient != null) {
            mNettyClient.destroy();
            mNettyClient = null;
        }
        if (connectReceiver != null) {
            unregisterReceiver(connectReceiver);
        }
    }

    private void initSocketClient(String serverIp, int port) {
        if (mNettyClient == null) {
            mNettyClient = new NettyTCPClient();
            mNettyClient.init(serverIp, port);
        }
    }



    @Subscribe
    public void getConnEvent(ConnEvent event) {
        if (event == null||viewModel == null) return;
        viewModel.connectNet(event.isConn);
    }

    @Subscribe
    public void getMsgEvent(MsgEvent event) {
        Log.i(CmdUtils.TAG, "event.msg===" + event.msg);
        if (event == null||viewModel == null) return;
        if (event.msg != null && event.msg.length() > 0) {

            try {
                if (CmdUtils.splitMsg(event.msg) != null && !CmdUtils.splitMsg(event.msg).isEmpty()) {
                    for (String cmd : CmdUtils.splitMsg(event.msg)) {
                        if (cmd.startsWith(Constant.MSG_LOAD())) {
                            viewModel.loadData(cmd);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void onSureClick(String serverIp, int port) {
        initSocketClient(serverIp,port);
    }

    @Override
    public void onOpenSetActivity() {
        if (mNettyClient != null) {
            mNettyClient.destroy();
            mNettyClient = null;
        }
    }
}
